﻿using UnityEngine;
using System.Collections;
using System;
public enum FireType
{
    PlayerFire,
    EnemyFire
}

public class PersonFire : MonoBehaviour 
{
    public GameObject destory_animation_Object { get; set; }

    public float out_time  {     get;   set;   }

    public float fire_move_speed;
    public Vector3 target_pos { get; set; }
    [Header("子弹的攻击力")]
    public int attack_point;

    public FireType type { get; set; }

    public void Set_Attack_Point(  int t )
    {
        attack_point = t;
    }

    

    public void FireMoveToTarget()
    {
        if (canFire &&  isFlying)
        {
            //Transform target = TurnBattleSystemManager.Instance.PlayersAttackTarget[0].transform;
            //Vector3 target_Pos = base.Target_pos;
            //炮弹发射速度
            float speed = fire_move_speed * Time.deltaTime;
            //GameObject fireObjcet_ = tm.players[pid].GetComponent<InstanceFightObject>().Make_Fight_Object();
            //transform.position = Vector3.MoveTowards(this.transform.position, target_Pos , step);
            //vecn = ( Target_pos   - this.transform.position ).normalized;
            transform.Translate(vecn * speed);

           // Debug.Log("开始执行FireMoveToTarget方法");
        }
    }

    public void SetTarget_Pos(Vector3 _t_pos)
    {
        target_pos = _t_pos;
        StartCoroutine(Wait_SetTargetVector3_StartMove());
    }
    /// <summary>
    /// 方向向量
    /// </summary>
    Vector3 vecn;
    /// <summary>
    /// 是否能发射子弹，，先要设定好方向向量
    /// </summary>
    bool canFire = false;

    /// <summary>
    /// 子弹是否在飞行中......
    /// </summary>
    bool isFlying = true ;

    public string target_tag;
    IEnumerator Wait_SetTargetVector3_StartMove()
    {
        vecn = (target_pos - this.transform.position).normalized;
      //  Debug.LogError("target_pos:" + target_pos  + "   this.transform.position:  "+ this.transform.position  + "   vecn:" + vecn); 

        yield return new WaitForSeconds(0.1f);
        canFire = true;
        //isFlying = true;
         //  Debug.Log("玩家单位--发射自动往--敌人方向--的方向向量是：" + vecn);
    }

    void InstantiateBaoZhaEffict(Transform ts)
    {
        /*生成爆炸效果*/
        Vector3 position = ts.position; // Position of the plane
        if (destory_animation_Object != null)
        {
            GameObject fire = Instantiate(destory_animation_Object, position, Quaternion.identity) as GameObject; // Set the bullet with Plane
        }
        //Destroy(fire, 0.8f);
    }

    void HideFire()
    {
        //if (   isFlying == false)
        //{

            SpriteRenderer s = GetComponent<SpriteRenderer>();
            s.sprite = null;
       // _timer.StopTimer();
             Destroy(this.gameObject, 0.8f);
        //}
    }

    // Use this for initialization
    void Start()
    {
        //CheckTimer();
       
    }


    void OnTriggerEnter2D(Collider2D otherCollider)
    {
      //  Debug.Log("OnTriggerEnter2D  PlayerFire out");
        if (otherCollider.CompareTag(target_tag) && isFlying)
        {

            //造成伤害
            try
            {
               // Debug.Log("攻击到了敌人，伤害值读取到的是："+ attack_point );
                EnemyInBattle e = otherCollider.GetComponent<EnemyInBattle>();
                e.GetDamage(  attack_point  );
            }
            catch
            {
                Debug.Log("查询EnemyInBattle对象失败，击中的不是敌人");
            }
            Debug.Log("OnTriggerEnter2D  PlayerFire");
            isFlying = false;
            Transform t = otherCollider.transform;
            //生成爆炸效果,如果没有则不生成
            InstantiateBaoZhaEffict(t);
            HideFire();
            TurnSystem.Instance.NextAction();
        }
    }

    Timer _timer;
    void CheckTimer()
    {
        //初始化计时器 
        _timer = new Timer(5);
        _timer.tickEvent += SendTurnNextAction;
        //10秒后开始发送信息
        _timer.StartTimer();
    }

    void SendTurnNextAction()
    {   
        Debug.Log("执行5秒之后的：SendTurnNextAction");
        TurnSystem.Instance.NextAction();
    }

    // Update is called once per frame
    void Update()
    {
        FireMoveToTarget();

        //_timer.UpdateTimer(Time.deltaTime);
       // HideFire();
    }



}
